package com.qp.common;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class DateKit {

    /**
     * 查询一周内的所有时间
     *
     * @param inNow
     * @return
     */
    public static List<Date> getCurrentWeek(Date inNow) {
        if (inNow == null) {
            return Collections.emptyList();
        }
        Date now = dayBegin(inNow);
        int idx = DateUtil.dayOfWeek(now);
        if (idx == 1) {
            idx = 7;
        } else {
            idx = idx - 1;
        }
        Date min = DateUtil.offsetDay(now, -(idx - 1));
        Date max = DateUtil.offsetDay(now, (6 - idx + 1));
        Long maxValue = max.getTime();
        List<Date> week = new ArrayList<>();
        Date temp = min;
        while (temp.getTime() < maxValue) {
            week.add(temp);
            temp = DateUtil.offsetDay(temp, 1);
        }
        return week;
    }

    /**
     * 获得某天最大时间 2020-02-19 23:59:59
     *
     * @param date
     * @return
     */
    public static Date dayEnd(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获得某天最小时间 2020-02-17 00:00:00
     *
     * @param date
     * @return
     */
    public static Date dayBegin(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    //格式（日期）
    private static DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * 格式（日期）
     *
     * @param ld
     * @return
     */
    public static String localParseDate(LocalDate ld) {
        if (ld == null) {
            return null;
        }
        return ld.format(formatterDate);
    }

    //格式（日期时间）
    private static DateTimeFormatter formatterDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 格式（日期时间）
     *
     * @param ld
     * @return
     */
    public static String localParseDateTime(LocalDateTime ld) {
        if (ld == null) {
            return null;
        }
        return ld.format(formatterDateTime);
    }

    /**
     * 差异值
     *
     * @param begin1
     * @param end1
     * @param begin2
     * @param end2
     * @return
     */
    public static Long minus(Date begin1, Date end1, Date begin2, Date end2) {
        if (begin1 == null || end1 == null || begin2 == null || end2 == null) {
            return 0L;
        }
        Long diff = end1.getTime() - begin1.getTime();
        if (diff <= 0) {
            return 0L;
        }
        Date maxBegin = begin1.getTime() < begin2.getTime() ? begin2 : begin1;
        Date minEnd = end1.getTime() > end2.getTime() ? end2 : end1;
        if (minEnd.getTime() <= maxBegin.getTime()) {
            return diff;
        }
        Long res = diff - (minEnd.getTime() - maxBegin.getTime());
        if (res < 0) {
            return 0L;
        }
        return res;
    }


    /**
     * 取得第几周，Hutool默认的可能有问题，没有设置FirstDayOfWeek
     *
     * @param date
     * @return
     */
    public static String weekOfYear(Date date) {
        int seq = DateTime.of(date).setFirstDayOfWeek(Week.MONDAY).weekOfYear();
        return "kw" + seq;
    }
}
